﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class AuthenticationSectionManager : ConfigurationManagerBase
	{
		private AuthenticationSection section;

		public AuthenticationSectionManager()
		{
			this.section = (AuthenticationSection)base.GetConfigurationSection("system.web/authentication");
		}

		private static string getPassword(FormsAuthPasswordFormat passwordFormat, string password)
		{
			if (string.IsNullOrEmpty(password))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "password");
			}
			switch (passwordFormat)
			{
				case FormsAuthPasswordFormat.Clear:
					return password;

				case FormsAuthPasswordFormat.SHA1:
					password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
					return password;

				case FormsAuthPasswordFormat.MD5:
					password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
					return password;
			}
			return password;
		}





		public void ModifyMode(AuthenticationMode mode)
		{
			this.section.Mode = mode;
			this.Save();
		}
		public AuthenticationSection GetAuthenticationSection()
		{
			return this.section;
		}





		public void ModifyForms(HttpCookieMode cookieless, string defaultUrl, string domain, bool enableCrossAppRedirects, string loginUrl, string name, string path, FormsProtectionEnum protection, bool requireSsl, bool slidingExpiration, TimeSpan timeout)
		{
			this.section.Forms.Cookieless = cookieless;
			if (string.IsNullOrEmpty(defaultUrl))
			{
				defaultUrl = "default.aspx";
			}
			this.section.Forms.DefaultUrl = defaultUrl;
			if (string.IsNullOrEmpty(domain))
			{
				domain = "";
			}
			this.section.Forms.Domain = domain;
			this.section.Forms.EnableCrossAppRedirects = enableCrossAppRedirects;
			if (string.IsNullOrEmpty(loginUrl))
			{
				loginUrl = "login.aspx";
			}
			this.section.Forms.LoginUrl = loginUrl;
			if (string.IsNullOrEmpty(name))
			{
				name = ".ASPXAUTH";
			}
			this.section.Forms.Name = name;
			if (string.IsNullOrEmpty(path))
			{
				path = "/";
			}
			this.section.Forms.Path = path;
			this.section.Forms.Protection = protection;
			this.section.Forms.RequireSSL = requireSsl;
			this.section.Forms.SlidingExpiration = slidingExpiration;
			this.section.Forms.Timeout = timeout;

			this.Save();
		}
		public FormsAuthenticationConfiguration GetForms()
		{
			return this.section.Forms;
		}






		public void ModifyFormsCredentials(FormsAuthPasswordFormat passwordFormat)
		{
			this.section.Forms.Credentials.PasswordFormat = passwordFormat;
			this.Save();
		}
		public FormsAuthenticationCredentials GetFormsCredentials()
		{
			return this.section.Forms.Credentials;
		}





		public void AddFormsCredentialsUser(string name, string password)
		{
			if (string.IsNullOrEmpty(name))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "name");
			}
			if (string.IsNullOrEmpty(password))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "password");
			}
			if (this.section.Forms.Credentials.Users[name] != null)
			{
				throw new ArgumentException("'" + name + "' 항목이 이미 추가되었습니다.", "name");
			}

			this.section.Forms.Credentials.Users.Add(new FormsAuthenticationUser(name, getPassword(this.section.Forms.Credentials.PasswordFormat, password)));
			this.Save();
		}
		public void ModifyFormsCredentialsUser(string name, string password)
		{
			if (string.IsNullOrEmpty(name))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "name");
			}
			if (string.IsNullOrEmpty(password))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "password");
			}
			if (this.section.Forms.Credentials.Users[name] == null)
			{
				throw new ArgumentException("'" + name + "' 항목이 존재하지 않습니다.", "name");
			}

			this.section.Forms.Credentials.Users[name].Password = getPassword(this.section.Forms.Credentials.PasswordFormat, password);
			this.Save();
		}
		public void RemoveFormsCredentialsUser(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				throw new ArgumentException("null 또는 빈문자열은 허용되지 않습니다.", "name");
			}
			if (this.section.Forms.Credentials.Users[name] == null)
			{
				throw new ArgumentException("'" + name + "' 항목이 존재하지 않습니다.", "name");
			}

			this.section.Forms.Credentials.Users.Remove(name);
			this.Save();
		}
		public void ClearFormsCredentialsUsers()
		{
			this.section.Forms.Credentials.Users.Clear();

			this.Save();
		}
		public FormsAuthenticationUser GetFormsCredentialsUser(string name)
		{
			return this.section.Forms.Credentials.Users[name];
		}
		public FormsAuthenticationUserCollection GetFormsCredentialsUserCollection()
		{
			return this.section.Forms.Credentials.Users;
		}


		public void ModifyPassport(string redirectUrl)
		{
			if (string.IsNullOrEmpty(redirectUrl))
			{
				redirectUrl = "internal";
			}
			this.section.Passport.RedirectUrl = redirectUrl;
			this.Save();
		}
		public PassportAuthentication GetPassportAuthentication()
		{
			return this.section.Passport;
		}
	}
}